$(function () {
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    return {
        dataGrid: null,
        form: null,
        flow: null,
        dom: {

        },
        init: function () {
            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired($('#bt_form_add, #bt_form_edit'));

            this.dataGrid = new xhDataGrid($('#list'), 'Flowtmp/getTemplate');
            this.dataGrid.parseToolbar().init();

            this.flow = new XhFlow($('.xh-flow-area'), 'flow', 'flow_test');
            this.flow.setDom({
                save: $('#bt_form_add, #bt_form_edit'),
                commit: $('#bt_form_submit')
            });

            this.listener();
        },
        listener: function () {
            var $this = this;
            //列表添加
            $('#bt_list_add').click(function(){
                $this.form.reset();
                $this.flow.clear().create('测试');
                xhExtra.forwardToForm(false);
            });
            //表单返回
            $('#bt_form_back').click(function() {
                xhExtra.backToList();
            });
            //表单新增
            $('#bt_form_add').click(function () {
                $this.funObj().submit(false);
            });
            //表单保存并提交
            $('#bt_form_submit').click(function () {
                $this.funObj().submit(true);
            });
            //双击行
            if ($('#bt_form_edit').length > 0) {
                $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                    $this.flow.getPower(rowData.id, function (power) {
                        xhExtra.ajaxRequest('Flowtmp/getTemplateInfo', {id: rowData.id}, {check: true}, function (data) {
                            $this.form.reset().fill(data);
                            xhExtra.forwardToForm(true);
                            $this.flow.clear().setBtnStatus(power);
                            $('#bt_form_add').hide();
                        });
                    });

                };
            }
        },
        funObj: function () {
            var $this = this;
            return {
                submit: function (_flowSubmit) {
                    $this.form.submit(baseUrl + 'Flowtmp/createTmp', {}, function (data) {
                        if (xhExtra.checkError(data)) return false;
                        if (!_flowSubmit) {
                            xhExtra.backToList($this.dataGrid);
                        }
                        else {
                            $this.flow.commit(data.msg);
                        }
                    });
                }
            }
        }
    }
}